package com.jinke.uang.ui.activity.mine;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.jinke.uang.R;
import com.jinke.uang.adapter.HelpAdapter;
import com.jinke.uang.base.BaseNormalActivity;
import com.jinke.uang.base.BaseResponse;
import com.jinke.uang.common.RouterMap;
import com.jinke.uang.constant.Constant;
import com.jinke.uang.bean.HelpCenterData;
import com.jinke.uang.constant.HttpConstant;
import com.jinke.uang.model.MineModule;
import com.jinke.uang.presenter.CommonPresenter;
import com.jinke.uang.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

@Route(name = "帮助中心", path = RouterMap.HELPCENTER_PAGE)
public class HelpCenterActivity extends BaseNormalActivity<CommonPresenter,MineModule>{

    @BindView(R.id.tv_email)
    TextView tvEmail;
    @BindView(R.id.tv_service_tel)
    TextView tvServiceTel;
    @BindView(R.id.rv)
    RecyclerView rv;
    private HelpAdapter reAdapter;

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
    }

    @Override
    protected void onUIReady() {
        rv.setLayoutManager(new LinearLayoutManager(this));
        DividerItemDecoration divider = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(this, R.drawable.custom_divider_help_center));
        rv.addItemDecoration(divider);
        reAdapter = new HelpAdapter(this);
        rv.setAdapter(reAdapter);

        mPresenter.getData(HttpConstant.HELP_CENTER,null);
        showLoadingView();
    }

    @Override
    protected int getContentResId() {
        return R.layout.activity_help_center;
    }

    @Override
    public String getPageTitle() {
        return getStringResource(R.string.help_center_page_title);
    }

    @Override
    protected CommonPresenter getPresenter() {
        return new CommonPresenter();
    }

    @Override
    protected MineModule getModule() {
        return new MineModule();
    }

    private void fillView(ArrayList<HelpCenterData> data) {
        List<HelpCenterData> list = new ArrayList<>();
        // TODO: 3是客服电话 4是邮箱
        for (int i = 0; i < data.size(); i++) {
            HelpCenterData helpData = data.get(i);
            if ("3".equals(helpData.getStatus())) {
                tvServiceTel.setText(helpData.getMessage());
            } else if ("4".equals(helpData.getStatus())) {
                tvEmail.setText(helpData.getMessage());
            } else {
                list.add(helpData);
            }
        }

        reAdapter.updateItems(list);
    }

    @OnClick(R.id.tv_service_tel)
    public void makingCall() {
        Intent intent = new Intent(Intent.ACTION_DIAL);
        Uri data = Uri.parse("tel:" + tvServiceTel.getText().toString());
        intent.setData(data);
        startActivity(intent);
    }

    @OnClick(R.id.tv_email)
    public void copyEmail() {
        // 得到剪贴板管理器
        ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            cm.setPrimaryClip(ClipData.newPlainText("text", tvEmail.getText().toString()));
            if (cm.hasPrimaryClip()) {
                cm.getPrimaryClip().getItemAt(0).getText();
            }
        } else{
            cm.setText(tvEmail.getText().toString());
        }
        ToastUtils.showShort(HelpCenterActivity.this, getStringResource(R.string.help_center_copy_succeed));
    }

    @Override
    public void showResponse(int apiType,String listType, Object o) {
        if(apiType== HttpConstant.HELP_CENTER){//帮助中心
            dismissLoadingView();
            BaseResponse<ArrayList<HelpCenterData>> helpCenterResponse = (BaseResponse<ArrayList<HelpCenterData>>) o;
            if (helpCenterResponse.getStatus() == Constant.HTTP_OK) {
                    fillView(helpCenterResponse.getData());
            } else {
                ToastUtils.showShort(HelpCenterActivity.this, helpCenterResponse.getMessage());
            }
        }
    }

    @Override
    public void showError(Throwable e) {
        dismissLoadingView();
    }
}
